// Copyright 2009 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Author: Gunnar Ritter

#include "cmdline/cmdline_test.h"
#include "cmdline/cmdline.h"
#include "probes/executive.h"

#define arraysize(x) (sizeof(x) / sizeof *(x))

namespace popdiag_cmdline {

static popdiag::ExecutiveOptions executive_options_;

void TestCmdline::CmdlineArgumentTest(int argc, const char **argv, int code) {
  Cmdline cmdline(argc, const_cast<char **>(argv));
  cmdline.SetErrorFile("/dev/null");
  int result = cmdline.Start();
  QVERIFY(result == code);
}

void TestCmdline::TestIllegalOption() {
  const char *argv[] = { "illegal_option", "-?", NULL };
  CmdlineArgumentTest(arraysize(argv) - 1, argv, EXIT_CODE_SYNTAX_ERROR);
}

void TestCmdline::TestMissingArgument() {
  const char *argv[] = { "missing_argument", "-u", NULL };
  CmdlineArgumentTest(arraysize(argv) - 1, argv, EXIT_CODE_SYNTAX_ERROR);
}

void TestCmdline::TestExcessArgument() {
  const char *argv[] = { "excess argument", "-u", "username", "-w", "password",
                         "superfluous", NULL };
  CmdlineArgumentTest(arraysize(argv) - 1, argv, EXIT_CODE_SYNTAX_ERROR);
}

void TestCmdline::TestMissingUsername() {
  const char *argv[] = { "missing_username", "-w", "password", NULL };
  CmdlineArgumentTest(arraysize(argv) - 1, argv, EXIT_CODE_SYNTAX_ERROR);
}

void TestCmdline::TestMissingPassword() {
  const char *argv[] = { "missing_password", "-u", "username", NULL };
  CmdlineArgumentTest(arraysize(argv) - 1, argv, EXIT_CODE_SYNTAX_ERROR);
}

void TestCmdline::TestNonwritableOutput() {
  // Directories are never writable so "-o ." must fail.
  const char *argv[] = { "nonwritable_output", "-o", ".",
                         "-u", "username", "-w", "password", NULL };
  CmdlineArgumentTest(arraysize(argv) - 1, argv, EXIT_CODE_RUNTIME_FAILURE);
}

void TestCmdline::TestAllArguments() {
  const char username[] = "user@test.example";
  const char password[] = "testpassword";
  const char imap_server[] = "imap.test.example";
  const char pop_server[] = "pop.test.example";
  const char smtp_server[] = "smtp.test.example";
  const char smtp_port_string[] = "123";
  int smtp_port = 123;
  const bool pull_all_mail = true;
  const char *argv[] = { "all_arguments", "-o", "/dev/null",
                         "-u", username, "-w", password,
                         "-i", imap_server, "-p", pop_server,
                         "-s", smtp_server, "-S", smtp_port_string,
                         "-a", "-q", "-d", NULL };
  CmdlineArgumentTest(arraysize(argv) - 1, argv, EXIT_CODE_SUCCESS);
  QCOMPARE(executive_options_.username, QString(username));
  QCOMPARE(executive_options_.password, QString(password));
  QCOMPARE(executive_options_.imap_server, QString(imap_server));
  QCOMPARE(executive_options_.pop_server, QString(pop_server));
  QCOMPARE(executive_options_.smtp_server, QString(smtp_server));
  QCOMPARE(executive_options_.smtp_port, smtp_port);
  QCOMPARE(executive_options_.pull_all_mail, pull_all_mail);
}

}  // namespace popdiag_cmdline

namespace popdiag {

// Fake methods from the Executive and Probe objects that we
// need for these tests, without actually starting probes.

Executive::Executive(QObject *parent, const ExecutiveOptions &options)
  : QObject(parent) {
    popdiag_cmdline::executive_options_ = options;
}

Executive::~Executive() {
}

void Executive::Start() {
}

void Executive::UpdateProgress() {
}

void Executive::ProbeDone() {
}

void Probe::PrintDebuggingInformation(bool /*doit*/) {
}

}  // namespace popdiag

QTEST_MAIN(popdiag_cmdline::TestCmdline)
